﻿using System;
using System.Globalization;
using System.Linq;

namespace Client.ValueConverters
{
    /// <summary>
    /// 从右到左，每隔X位插入一个指定的符号进行分隔。
    /// </summary>
    public class StringFormat_SplitByIntervalAndJoin_Reverse_Converter : System.Windows.Data.IValueConverter
    {
        /// <summary>
        /// 默认分隔符 空格
        /// </summary>
        public const int DefaultInterval = 4;

        /// <summary>
        /// 默认分隔符 空格
        /// </summary>
        public const string DefaultSymbol = " ";

        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter">interval;symbol 例子4;*</param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return string.Empty;
            }

            #region 解析参数
            
            int interval = DefaultInterval;
            string symbol = DefaultSymbol;

            if (parameter != null)
            {
                string strParameter = parameter.ToString();
                int p = strParameter.IndexOf(";");

                try
                {
                    string strInterval = strParameter.Substring(0, p);
                    interval = int.Parse(strInterval);
                }
                catch
                { 
                
                }

                try
                {
                    var t = strParameter.Substring(p + 1);
                    if (t.Length > 0)
                    {
                        symbol = t;
                    }
                }
                catch
                { 
                
                }

            }

            #endregion

            string r = value.ToString();
            return this.Format_SplitByIntervalAndJoin_Reverse(r, interval, symbol);
        }

        /// <summary>
        /// 从右到左，每隔X位插入一个指定的符号进行分隔。
        /// </summary>
        /// <param name="input">原始字符串。</param>
        /// <param name="interval">分割间隔。</param>
        /// <param name="symbol">用于分隔的符号，默认为空格。</param>
        /// <returns>处理后的字符串。</returns>
        public string Format_SplitByIntervalAndJoin_Reverse(string input, int interval, string symbol)
        {
            if (string.IsNullOrEmpty(input)) return input; // 处理空字符串情况
            if (interval <= 0) throw new ArgumentException("Interval must be greater than 0.", nameof(interval));

            var k = (int)Math.Ceiling((double)input.Length / interval); // 计算需要分成几部分

            var l = Enumerable.Range(0, k)
                .Select(i => input.Substring(Math.Max(0, input.Length - (i + 1) * interval), Math.Min(interval, input.Length - i * interval)))
                .Reverse();

            return string.Join(symbol, l);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

